package com.qijian.common.utils.uuid;

import cn.hutool.core.util.IdUtil;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * ID生成器工具类
 *
 * @author ruoyi
 */
public class IdUtils extends IdUtil {
    /**
     * 简化雪花ID算法的单例获取
     *
     * @return Long
     */
    public static Long singletonSnowId() {
        return IdUtil.getSnowflake(1, 1).nextId();
    }

    /**
     * 生成实体的编码
     *
     * @param pre /
     * @return /
     */
    public static String getNumber(String pre, Object suffix) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyMMdd");
        LocalDate date = LocalDate.now();
        String dateStr = date.format(fmt);
        //后缀保留4位，不足补0
        String suffixStr = String.format("%4s", suffix).replaceAll(" ", "0");
        return pre + dateStr + suffixStr;
    }

    public static String getNumber(String pre, Object suffix, int suffixLength) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyMMdd");
        LocalDate date = LocalDate.now();
        String dateStr = date.format(fmt);
        //后缀保留4位，不足补0
        String len = "%" + suffixLength + "s";
        String suffixStr = String.format(len, suffix).replaceAll(" ", "0");
        return pre + dateStr + suffixStr;
    }

    /**
     * 设置Code长度
     *
     * @param pre         /
     * @param snowflakeId /
     * @param num         /
     * @return /
     */
    public static String getCode(String pre, Long snowflakeId, int num) {
        String id = snowflakeId.toString();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyMMdd");
        LocalDate date = LocalDate.now();
        String dateStr = date.format(fmt);
        return pre + "-" + dateStr + "-" + id.substring(id.length() - num);
    }
}
